<template>
    <div class="content-wrapper-view">
		<div class="content-wrapper-body"><!-- 开始 -->

        <div class="filter-wrapper">
            <el-form ref="filterForm" :model="listQuery" :inline="true">
                <el-form-item prop="openid" size="medium">
                    <el-input v-model="listQuery.openid" placeholder="名称"></el-input>
                </el-form-item>

                <el-form-item prop="status" size="medium">
                    <el-select v-model="listQuery.sex" placeholder="性别" clearable style="width: 100px">
                        <el-option
                                v-for="item in sexOptions"
                                :key="item.key"
                                :label="item.label"
                                :value="item.key">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-button type="primary" size="medium" icon="el-icon-search" @click="handleFilter">搜索</el-button>
                <el-button type="primary" size="medium" icon="el-icon-mars-reset" @click="handleResetFilter">重置</el-button>
                <el-button type="danger" size="medium" icon="el-icon-delete" @click="handleBatchDelete">批量删除</el-button>
                <el-button type="primary" size="medium" icon="el-icon-delete" @click="sync">同步远程用户</el-button>
            </el-form>

        </div>

        <el-table
                :data="list"
                border fit
                highlight-current-row
                v-loading="listLoading"
                element-loading-text="给我一点时间"
                @selection-change="handleSelectionChange">

            <el-table-column
                    type="selection"
                    width="55" align="center">
            </el-table-column>
            <el-table-column type="index" width="55" align="center"></el-table-column>

            <el-table-column label="头像" width="100px" align="center">
                <template slot-scope="scope">
                    <img v-if="scope.row.headimgurl" :src="scope.row.headimgurl" alt="" style="width: 46px; height: 46px;">
                </template>
            </el-table-column>

            <el-table-column label="昵称" width="130px" align="center">
                <template slot-scope="scope"> {{decodeURI(scope.row.nickname)}} </template>
            </el-table-column>

            <el-table-column label="OPENID" width="250px" align="center">
                <template slot-scope="scope"> {{scope.row.openid}} </template>
            </el-table-column>

            <el-table-column label="性别" align="center">
                <template slot-scope="scope">
                    <el-tag type="success" v-if="scope.row.sex === 1">男</el-tag>
                    <el-tag type="danger" v-else>女</el-tag>
                </template>
            </el-table-column>

            <el-table-column label="国家" align="center">
                <template slot-scope="scope"> {{scope.row.country}} </template>
            </el-table-column>

            <el-table-column label="省份" align="center">
                <template slot-scope="scope"> {{scope.row.province}} </template>
            </el-table-column>

            <el-table-column label="城市" align="center">
                <template slot-scope="scope"> {{scope.row.city}} </template>
            </el-table-column>

            <el-table-column label="分组ID" align="center">
                <template slot-scope="scope"> {{scope.row.groupid}} </template>
            </el-table-column>

            <el-table-column label="标签" align="center">
                <template slot-scope="scope"> {{scope.row.tagidList}} </template>
            </el-table-column>

            <el-table-column label="关注时间" width="150px" align="center">
                <template slot-scope="scope"> {{scope.row.ctime | parseTime('{y}-{m}-{d} {h}:{i}')}} </template>
            </el-table-column>

            <el-table-column label="操作" width="180px" align="center">
                <template slot-scope="scope">
                    <el-button size="small" type="danger" icon="delete" @click="handleDelete(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <div v-show="!listLoading" class="pagination-container">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="listQuery.page"
                    :page-sizes="[10, 20, 30, 50]"
                    :page-size="listQuery.rows"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </div>
    </div>
    </div>
</template>

<script>
    import { created, updated, deleted, deletedBatch, selectOne, selectList, syncUserList } from '@/api/weixin/subscribeUser'

    const sexOptions = [{label: '男', key: 1}, {label: '女', key: 2}]

    export default {
        name: 'SubscribeUser',
        data() {
            return {
                list: [],
                total: null,
                listLoading: false,
                multipleSelection: [],
                listQuery: {
                    page: 1,
                    rows: 10,
                    openid: undefined,
                    orderBy: 'ctime desc'
                },
                dialogFormVisible: false,
                dialogStatus: '',
                textMap: {
                    update: '编辑',
                    create: '创建'
                },
                sexOptions

            }
        },
        created() {
            this.getTableData()
        },
        methods: {
            handleFilter() {//条件过滤
                this.listQuery.page = 1
                this.getTableData()
            },
            handleResetFilter() {//重置过滤条件
                this.$refs.filterForm.resetFields()
                this.getTableData()
            },
            handleDialogClose() {//关闭窗口回调函数
                this.resetForm()
            },
            handleSelectionChange(val) {//多选
                this.multipleSelection = val;
            },
            handleSizeChange(val) {
                this.listQuery.rows = val
                this.getTableData()
            },
            handleCurrentChange(val) {
                this.listQuery.page = val
                this.getTableData()
            },
            handleDelete(row) {//删除数据
                this.$confirm('确定要删除？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    deleted(row.id).then(response => {
                        if(response.data.status) {
                            this.$notify({
                                title: '成功', message: response.data.message,
                                type: 'success', duration: 2000
                            })
                            this.getTableData()
                        } else {
                            this.$notify({
                                title: '发生错误', message: response.data.message,
                                type: 'error', duration: 2000
                            })
                        }
                    })
                }).catch(() => {
                    this.$message({ type: 'warning', message: '已取消删除' })
                })
            },
            handleBatchDelete() {//批量删除数据
                this.$confirm('确定要删除？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    if(this.multipleSelection.length > 0) {
                        let ids = []
                        for(let item of this.multipleSelection) {
                            ids.push(item.id)
                        }
                        deletedBatch({ids: ids.join(',')}).then(response => {
                            if(response.data.status) {
                                this.$notify({
                                    title: '成功', message: response.data.message,
                                    type: 'success', duration: 2000
                                })
                                this.getTableData()
                            } else {
                                this.$notify({
                                    title: '发生错误', message: response.data.message,
                                    type: 'error', duration: 2000
                                })
                            }
                        })

                    } else {
                        this.$notify({
                            title: '提示', message: '请选择需要删除的记录',
                            type: 'warning', duration: 2000
                        })
                    }
                }).catch(() => {
                    this.$message({ type: 'info', message: '已取消删除' })
                })
            },
            getTableData() {
                this.listLoading = true
                selectList(this.listQuery).then(response => {
                    this.list = response.data.data.list

                    this.total = response.data.data.total
                    this.listLoading = false
                }).catch(error => {
                    console.log(error)
                    this.listLoading = false
                })
            },
            sync() {
                this.listLoading = true
                syncUserList().then(response => {
                    if(response.status) {
                        this.getTableData()
                    }
                })
            }
        }
    }
</script>

<!-- 局部样式 -->
<style rel="stylesheet/scss" lang="scss" scoped>

</style>
